package com.gzz.orchid.common.webmvc.support.limiter;


import com.gzz.orchid.common.webmvc.support.limiter.access.AccessLimiterAspect;
import com.gzz.orchid.common.webmvc.support.limiter.manager.ILimiterManager;
import com.gzz.orchid.common.webmvc.support.limiter.manager.RedisLimitManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author higmd
 * <p>
 * ## AutoConfiguration
 * org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
 * com.gzz.canyon.common.boot.limiter.LimiterConfigure,\
 * com.gzz.canyon.common.boot.limiter.aop.LimitAspect
 */
@Slf4j
@Configuration
public class LimiterConfiguration {

    @Bean
    public ILimiterManager limiterManager(StringRedisTemplate stringRedisTemplate) {
        log.info("启用限流...............");
        //        return new GuavaLimitManager();
        return new RedisLimitManager(stringRedisTemplate);
    }


    /**
     * 访问限流切面
     */
    @Bean
    @ConditionalOnBean(value = {RedisLimitManager.class})
    public AccessLimiterAspect accessLimiterAspect() {
        log.info("启用限流切面...............");
        return new AccessLimiterAspect();
    }
}
